package org.dingyom.aftermarket.controller;

import org.dingyom.aftermarket.service.IGuaranteeItemService;
import org.dingyom.aftermarket.domain.GuaranteeItem;
import org.dingyom.aftermarket.query.GuaranteeItemQuery;
import org.dingyom.basics.util.AjaxResult;
import org.dingyom.basics.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/guaranteeItem")
public class GuaranteeItemController {
    @Autowired
    public IGuaranteeItemService guaranteeItemService;


    /**
     * 保存和修改公用的
     * @param guaranteeItem  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody GuaranteeItem guaranteeItem){
        try {
            if( guaranteeItem.getId()!=null)
                guaranteeItemService.modify(guaranteeItem);
            else
                guaranteeItemService.add(guaranteeItem);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("保存对象失败！"+e.getMessage());
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            guaranteeItemService.delete(id);
            return AjaxResult.me();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.me().setMessage("删除对象失败！"+e.getMessage());
        }
    }
	
    //获取用户
    @GetMapping("/{id}")
    public AjaxResult get(@PathVariable("id")Long id)
    {
        try {
            GuaranteeItem guaranteeItem = guaranteeItemService.getOne(id);
            return AjaxResult.me().setData(guaranteeItem);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("获取一个失败！"+e.getMessage());
        }
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping
    public AjaxResult list(){

        try {
            List< GuaranteeItem> list = guaranteeItemService.getAll();
            return AjaxResult.me().setData(list);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("获取所有失败！"+e.getMessage());
        }
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping
    public AjaxResult json(@RequestBody GuaranteeItemQuery query)
    {
        try {
            PageList<GuaranteeItem> pageList = guaranteeItemService.getPage(query);
            return AjaxResult.me().setData(pageList);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("获取分页数据失败！"+e.getMessage());
        }
    }

    /**
    * 批量删除
    * @param ids
    * @return
    */
    @PatchMapping
    public AjaxResult batchDel(@RequestBody List<Long> ids){
        try {
            guaranteeItemService.batchDel(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("删除失败"+e.getMessage());
        }
    }
}
